import uuid from 'uuid'

export default {
  watch: {
    $route(value) {
      this.init()
    },
    selectedIds(arr) {
      if (!arr.length) {
        this.getPnpDownloadStatus(true)
      } else {
        this.stop()
      }
    }
  },
  beforeMount() {
    this.init()
  },
  beforeDestroy() {
    this.stop()
  },
  methods: {
    getName(row) {
      let Venid = (row.Venid && row.Venid.toString(16).toUpperCase() || '0').padStart(4,"0")
      let Devid = (row.Devid && row.Devid.toString(16).toUpperCase() || '0').padStart(4,"0")
      let Subsysid = (row.Subsysid && row.Subsysid.toString(16).toUpperCase() || '0').padStart(8,"0")
      let Revid = (row.Revid && row.Revid.toString(16).toUpperCase() || '0').padStart(2,"0")
      return ['VEN_' + (Venid || ''), 'DEV_' + (Devid || ''), 'SUBSYS_' + (Subsysid || ''), 'REV_' + (Revid || '')].join('&')
    },
    init() {
      this.setAreaId()
      if (this.areaId) {
        this.copyForm({
          content: '',
          type: -1
        })
        this.getPage(1)
      }
    },
    stop() {
      this.enable = false
      clearTimeout(this.setTimeoutKey)
    },
    sortChange({ column, key, order }) {
      let orderMap = {
        desc: 2,
        asc: 1,
        normal: 0
      }

      let type = orderMap[order]
      this.page.sort = {
        Field: key,
        Order: type
      }

      this.getPage(1)
    },
    getPnpDownloadStatus(force) {
      if (force) {
        this.enable = force
      }

      clearTimeout(this.setTimeoutKey)
      if (!this.isEnable()) {
        return
      }

      let fn = () => {        
        this.setTimeoutKey = setTimeout(() => {
          if (!this.isEnable()) {
            return
          }

          let page = this.page
          let param = {
            Pid: this.areaId,
            PageSize: page.pageSize,
            PageIndex: page.pageNum,
            OCond: page.sort
          }
          let obj = {
            network: api.getNetworkCardListProgress,
            gpu: api.getGraphicsCardListProgress,
            voice: api.getAudioCardListProgress
          }

          let originForm = this.originForm
          let type = originForm.type
          let content = originForm.content.trim()
          param.Param = content
          if (type != -1) {
            param.OnlyUnMake = true
          }

          this.resetCheckboxList()
          let pnpName = this.pnpName
          obj[pnpName](param).then(res => {
            let data = res.Data
            let list = data.List || []
            page.list = list

            let count = 0
            list.forEach(item => {
              item.uuid = uuid()
              if (item.Status == 3) {
                ++count
              }
            })
            this.completeCount = count
            page.total = data.Total
            this.setCheckboxMap()

            this.getPnpDownloadStatus()
          }).catch(() => {
            this.completeCount = 0
            if (!this.isEnable()) {
              return
            }
            this.getPnpDownloadStatus()
          })
        }, 3000)
      }

      fn()
    },
    isEnable() {
      if (!this.enable || this.$route.name != this.routerName) {
        this.stop()
        return false
      } else {
        return true
      }
    },
    getPage(pageNum = 1) {
      if (!this.hasPermissionPage) {
        this.util.toMain()
        return
      }

      clearTimeout(this.setTimeoutKey)
      this.loading = true
      let obj = {
        network: api.getNetworkCardList,
        gpu: api.getGraphicsCardList,
        voice: api.getAudioCardList
      }

      let page = this.page
      page.pageNum = pageNum
      let param = {
        Pid: this.areaId,
        PageSize: page.pageSize,
        PageIndex: pageNum,
        OCond: page.sort
      }

      let originForm = this.originForm
      let type = originForm.type
      let content = originForm.content.trim()
      param.Param = content
      if (type != -1) {
        param.OnlyUnMake = true
      }

      page.list = []
      this.resetCheckboxList()
      let pnpName = this.pnpName
      obj[pnpName](param).then(res => {
        this.loading = false
        let data = res.Data
        let list = data.List || []

        let count = 0
        list.forEach(item => {
          item.uuid = uuid()
          if (item.Status == 3) {
            ++count
          }
        })
        this.completeCount = count
        page.total = data.Total

        page.list = list
        this.setCheckboxMap()

        this.getPnpDownloadStatus()
      }).catch(() => {
        this.loading = false
        this.completeCount = 0
        
        // 重置第一页
        this.page.pageNum = 1
        this.getPnpDownloadStatus()
      })
    },
    // 终止更新或修复
    stopPnp() {
      this.loading = true
      api.stopPnpUpdateOrFix({
        Gids: [this.tempItem.Gid],
        Pid: this.areaId
      }).then(res => {
        this.tipUtil.success('操作成功')
        this.getPage(this.page.pageNum)
      }).catch(() => {
        this.loading = false
      })
    },
    DeletePnp(tempItem) {
      let pnptype = {
        gpu: 0,
        network: 1,
        voice: 2
      }
      let param = {
        Pid: this.areaId,
        Pt: pnptype[this.pnpName],
        Items: [tempItem]
      }

      api.deletePnp(param).then(res => {
        this.tipUtil.success('操作成功')
        this.getPage(this.page.pageNum)
      }).catch(() => {
        this.loading = false
      })
    },
    FixPnp(tempItem) {
      let pnptype = {
        gpu: 0,
        network: 1,
        voice: 2
      }
      let param = {
        Pid: this.areaId,
        Pt: pnptype[this.pnpName],
        Gids: [tempItem.Gid]
      }
      api.repairPnpDrive(param).then(res => {
        this.tipUtil.success('操作成功')
        this.getPage(this.page.pageNum)
      }).catch(() => {
        this.loading = false
      })
    },
    toBatchOperate(type) {
      if (!this.selectedIds.length) {
        this.tipUtil.warning('请至少选择一条数据')
        return
      }

      this.operationType = type
      this.showBatchModal = true
    }
  }
}